﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class per_bank_card : MpageBase
{
    protected Model.CMember cmember = null;
    protected List<Model.AddressProvince> AddressProvince = null;
    protected List<Model.AddressCity> AddressCity = null;
    protected Model.SF_u_Pay addbankcard = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            databind();
        }
    }

    protected void databind()
    {
        int total1 = 0;
        AddressProvince = BLL.AddressProvince.GetPageList_Where(0, int.MaxValue, 0, out total1);
        ddlProvince.DataSource = AddressProvince;
        ddlProvince.DataTextField = "Name";
        ddlProvince.DataValueField = "Id";
        ddlProvince.DataBind();

        int total2 = 0;
        AddressCity = BLL.AddressCity.GetPageList_Where(0, int.MaxValue, 1, out total2);
        ddlCity.DataSource = AddressCity;
        ddlCity.DataTextField = "Name";
        ddlCity.DataValueField = "Id";
        ddlCity.DataBind();
        addbankcard = BLL.SF_u_Pay.GetModelByUserId(loginUserInfo.Id);
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("-请选择(城市)-", "-1"));
        ddlCity.DataBind();
         

        if (int.Parse(ddlProvince.SelectedValue) != -1)
        {
            int total = 0;
            AddressCity = BLL.AddressCity.GetPageList_Where(0, int.MaxValue, int.Parse(ddlProvince.SelectedValue), out total);
            ddlCity.DataSource = AddressCity;
            ddlCity.DataTextField = "Name";
            ddlCity.DataValueField = "Id";
            ddlCity.DataBind();
             

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (loginUserInfo != null && loginUserInfo.Id > 0)
        {
            cmember = BLL.CMember.GetModel(loginUserInfo.Id);
            if (cmember.zhifuPassword.Equals(BLL.Security.MD5(zhifupass.Text.Trim())))
            {
                if (addbankcard == null)
                {
                    Model.SF_u_Pay newaddbankcard = new Model.SF_u_Pay();
                    newaddbankcard.UserID = loginUserInfo.Id;
                    newaddbankcard.AccountName = loginUserInfo.realname;
                    newaddbankcard.AccountNumber = cardId.Text.Trim();
                    newaddbankcard.AddTime = DateTime.Now;
                    newaddbankcard.BankID = long.Parse(BankName.SelectedValue.ToString());
                    newaddbankcard.BankName = BankName.SelectedItem.Text;
                    newaddbankcard.BranchName = "";
                    newaddbankcard.Province = ddlProvince.SelectedItem.Text;
                    newaddbankcard.City = ddlCity.SelectedItem.Text;
                    if (BLL.SF_u_Pay.Add(newaddbankcard) > 0)
                    {
                        alertmessageAndGoback("添加银行卡成功！");
                    }
                    else
                    {
                        alertmessageAndGoback("添加失败！");
                    }
                }
                else
                {
                    addbankcard.AccountNumber = cardId.Text.Trim();
                    addbankcard.BankID = long.Parse(BankName.SelectedValue.ToString());
                    addbankcard.BankName = BankName.SelectedItem.Text;
                    addbankcard.Province = ddlProvince.SelectedItem.Text;
                    addbankcard.City = ddlCity.SelectedItem.Text;
                    if (BLL.SF_u_Pay.Update(addbankcard) > 0)
                    {
                        alertmessageAndGoback("更新成功！");
                    }
                    else
                    {
                        alertmessageAndGoback("更新失败！");
                    }
                }
            }
            else
            {
                alertmessageAndGoback("支付密码错误！");
            }

        }
    }
}